<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Admin\Controller;
use Think\Controller;

class PcontentController extends BaseController {

	public function content_add(){
		$plate_id = I('get.id');
		$this->assign('plate_id', $plate_id);
		$this->assign('plate_info', $this->get_palte_info($plate_id));
		$edit = I('get.edit');
		if($edit == 'true'){
			$edit_id = I('get.edit_id');
			$this->assign('edit', $edit);
			$this->assign('article_id', $edit_id);
			$this->assign('article_info', $this->get_article_info($edit_id));
		}
		$this->display();
	}

	/**
	 * 根据资源ID获取当前资源信息
	 */
	protected function get_article_info($edit_id = null){
		$p_content = D(C('TABLE_PLATECONTENT'));
		$res = $p_content->find($edit_id);
		return $res;
	}

	/**
	 * 添加文档
	 */
	public function p_add(){
		$sesion = session('userinfo');
		$rules = array(
			array('title','require','标题不能为空！'),
			array('title','','已经存在此标题！',0,'unique',1),
		);
		$p_content = D(C('TABLE_PLATECONTENT'));
		if(!$p_content->validate($rules)->create()){
			$this->error($p_content->getError(),'',1);
		}else{
			$extendattrs = "";
			foreach(I('post.extendattr') as $v){
				$extendattrs .= $v.",";
			}
			$p_content->extendattr = rtrim($extendattrs,',');
			$content 			   = html_entity_decode(I('post.content'));
			$p_content->videopath  = html_entity_decode(I('post.videopath'));
			$p_content->audiopath  = html_entity_decode(I('post.audiopath'));
			$p_content->content    = $content;
			$p_content->description= cutArticle($content, 50);
			$p_content->u_date     = date("Y-m-d H:i:s", time());
			$p_content->userid     = $sesion['id'];
			$p_content->editdate   = date("Y-m-d H:i:s", time());
			$result = $p_content->add();
			if($result){
				$this->success('添加成功');
			}else{
				$this->error('添加失败','',1);
			}
		}
	}

	/**
	 * 编辑文档
	 */
	public function p_edit(){
		$rules = array(
			array('title','require','标题不能为空！'),
		);
		$p_content = D(C('TABLE_PLATECONTENT'));
		if(!$p_content->validate($rules)->create()){
			$this->error($p_content->getError(),'',1);
		}else{
			$extendattrs = "";
			foreach(I('post.extendattr') as $v){
				$extendattrs .= $v.",";
			}
			$p_content->extendattr = rtrim($extendattrs,',');
			$content 			   = html_entity_decode(I('post.content'));
			$p_content->videopath  = html_entity_decode(I('post.videopath'));
			$p_content->audiopath  = html_entity_decode(I('post.audiopath'));
			$p_content->content    = $content;
			$p_content->description= cutArticle($content, 50);
			$p_content->editdate    = date("Y-m-d H:i:s", time());
			$result = $p_content->where(array('id' => I('post.plate_article_id')))->save();
			if($result){
				$this->success('编辑成功', U('Manages/plate'));
			}else{
				$this->error('编辑失败','',1);
			}
		}
	}

	/**
	 * 资源内容列表
	 */
	public function content_list($id = null){
		$this->get_plateid_contentlist($id);
		$this->display();
	}

	/**
	 * 根据版块ID获取内容列表
	 */
	protected function get_plateid_contentlist($plate_id = null){
		$p_content = D(C('TABLE_PLATECONTENT'));
		$where['plate_id'] = $plate_id;
		$count = $p_content->where($where)->count();
		//每页显示条数
		$num = 12;
		$page = new \Think\Page($count,$num);
		$obj = pagestyle($page);
		$show = $obj->show();
		$result = $p_content->where($where)->order('editdate DESC,id desc')->limit(limitbakstr($obj))->select();
		if($result){
			foreach($result as &$v){
				$v['plate_info'] = $this->get_palte_info($plate_id);
			}
			$this->assign('plate_list', $result);
			$this->assign('page',$show);
		}
	}

	/**
	 * 根据版块ID获取当前版块的信息
	 */
	protected function get_palte_info($plate_id = null){
		$result = null;
		$p_mages = D(C('TABLE_PLATEMANAGES'));
		$res = $p_mages->field('plate_name')->find($plate_id);
		if($res) $result = $res['plate_name'];
		return $result;
	}

	/*
	 * 编辑文档
	 */
	public function edit($id=null){
		$id = htmlspecialchars($id);
		if(!empty($id)){
			$this->methodcontrol($id,'save','编辑成功','编辑失败',U('Index/articlelist'));
		}else{
			$this->error('非法参数！','',1);
		}
	}
	/*
	 * 添加、编辑公用方法
	 */
	protected function methodcontrol($id=null,$method,$success_message,$error_message,$bakurl){
		$rules = array(
				array('title','require','标题不能为空！'),
		);
		$article = D(C('TABLE_ARTICLE'));
		if(!$article->validate($rules)->create()){
			$this->error($article->getError(),'',1);
		}else{
			$extendattrs = "";
			foreach(I('post.extendattr') as $v){
				$extendattrs .= $v.",";
			}
			$content 			 = html_entity_decode(I('post.content'));
			if($method == 'add') $article->pv    = mt_rand(120,498);
			$article->videopath  = html_entity_decode(I('post.videopath'));
            $article->audiopath  = html_entity_decode(I('post.audiopath'));
			$article->content    = $content;
			$article->description= cutArticle($content, 50);
			$article->extendattr = rtrim($extendattrs,',');
			$article->editdate 	 = date("Y-m-d H:i:m");
			if($method == 'save') $article->pubdate = I('post.pubdate');
			$where['id'] = !empty($id) ? $id : null;
			$result = $article->where($where)->$method();
			if($result !== false){
				//A(C('TABLE_LOG'))->index(1,'添加文档成功');
				$this->success($success_message,$bakurl);
			}else{
				//A(C('TABLE_LOG'))->index(0,'添加文档失败');
				$this->error($error_message,'',1);
			}
		}
	}


}